//
//  SearchMenusVC.m
//  Menu
//
//  Created by mc on 16/4/22.
//  Copyright © 2016年 xmj. All rights reserved.
//

#import "SearchMenusVC.h"
#import "MenusResultVC.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"

/** 标题字体 */
#define kNameFont   [UIFont systemFontOfSize:16]
/** 正文字体 */
#define kTextFont   [UIFont systemFontOfSize:14]

@interface SearchMenusVC ()<UISearchBarDelegate>
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, copy) NSString *searchTitle;
@property (nonatomic, strong) NSDictionary *dict;

@end

@implementation SearchMenusVC
static NSString * const REQUESTURL = @"http://apis.haoservice.com/lifeservice/cook/query?";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *rightBtn =[ [UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchMenus)];
    self.navigationItem.rightBarButtonItem = rightBtn;
      //self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
      CGFloat textFieldW = [UIScreen mainScreen].bounds.size.width - 70;
      self.textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, textFieldW, 28)];
      self.textField.backgroundColor = [UIColor whiteColor];
      self.textField.placeholder = @" 请输入关键词搜索";
      [self.textField setValue:kTextFont forKeyPath:@"_placeholderLabel.font"];
    self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, self.textField.frame.size.height)];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.layer.cornerRadius = 5;
    
    [self.textField isFirstResponder];
    self.navigationItem.titleView = self.textField;
    
    UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
    titlelabel.text = @"热门搜索";
    titlelabel.textColor = [UIColor blackColor];
    titlelabel.font = kNameFont;
    [self.view addSubview:titlelabel];
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(titlelabel.frame), 50, 25)];
    [btn1 setTitle:@"番茄" forState:UIControlStateNormal];
    btn1.titleLabel.font = kTextFont;
    [btn1 setTitleColor:[UIColor colorWithRed:196.0/255 green:199.0/255 blue:202.0/255 alpha:1] forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor colorWithRed:196.0/255 green:199.0/255 blue:202.0/255 alpha:0.3];
    [btn1 addTarget:self action:@selector(showMenus:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
}

- (void)loadData{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = NSLocalizedString(@"Loading...", @"HUD loading title");

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"menu"] = self.searchTitle;
    params[@"pn"] = @"1";
    params[@"rn"] = @"4";
    params[@"key"] = @"3c286dc4f120495199b4503dbb52b835";
    //params[@"key"] = @"11";
    __weak typeof(self) weakSelf = self;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    
    [manager GET:REQUESTURL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        weakSelf.dict = responseObject[@"result"];
        
        if (self.dict == nil) {
            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"" message:@"没有查到您要的信息！" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
            [alertView addAction:cancelAction];
            [self presentViewController:alertView animated:(YES) completion:^{
            }];
            return;
        }
        MenusResultVC *searchMenusVC = [[MenusResultVC alloc] init];
        searchMenusVC.dict = self.dict;
        self.hidesBottomBarWhenPushed=YES;
        [self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]];
        self.navigationItem.title = @"";
        [self.navigationController pushViewController: searchMenusVC animated: YES];
        self.hidesBottomBarWhenPushed=NO;
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         [MBProgressHUD hideHUDForView:self.view animated:YES];
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
        [alertView addAction:cancelAction];
        [self presentViewController:alertView animated:(YES) completion:^{
            
        }];
        
    }];
    
}



- (void)showMenus:(UIButton *)btn{
    self.searchTitle = btn.currentTitle;
     [self loadData];
    }

- (void)searchMenus{
    self.searchTitle = self.textField.text;
     [self loadData];
}


@end
